<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>密码登录</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
           crossorigin="anonymous">

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"
            crossorigin="anonymous"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            crossorigin="anonymous"></script>
</head>
<body background="/img/login.jpg" style="background-repeat: no-repeat;background-size: cover">
<div class="container">
    <div class="row" style="margin-top: 200px;">
        <div class="col-md-4 col-md-offset-4">
            <div style="margin:10px 10px 10px 10px">

                <!-- 输入手机号-->
                <div id="phone_div" class="form-group input-group-lg">
                    <label style="font-size: 16px;" class="control-label">手机号</label>
                    <input id="phone" type="tel" class="form-control" placeholder="请输入手机号">
                    <span id="message1" class="help-block" style="height: 12px;"></span>
                </div>


                <!-- 输入密码-->
                <div id="password_div" class="form-group input-group-lg">
                    <label style="font-size: 16px;" class="control-label">密码</label>
                    <input id="password" type="password" class="form-control" placeholder="请输入密码">
                    <span id="message2" class="help-block" style="height: 12px;"></span>
                </div>

                <button id="sub_btn" type="submit" class="btn btn-default btn-lg btn-block">登录</button><br>
                <div class="row">
                    <div class="col-md-4">
                        <a href="index.jsp" class="button"><span
                                class="glyphicon glyphicon-grain glyphicon glyphicon-">返回主页</span></a>
                    </div>
                    <div class="col-md-4 col-md-offset-4">
                        <a href="login.jsp" class="button"><span
                                class="glyphicon glyphicon-grain glyphicon glyphicon-">验证码登录</span></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        //恢复输入框状态
        //用户点击输入手机号时，恢复输入框状态
        $("#phone").click(function () {
            $("#phone_div").removeClass("has-error");
            $("#message1").text("");
        });
        //用户点击输入密码时，恢复输入框状态
        $("#password").click(function () {
            $("#password_div").removeClass("has-error");
            $("#message2").text("");
        });

        $("#sub_btn").click(function () {
            //验证手机号格式
            var phone = $("#phone").val();
            var regex = /^(13[0-9]{9})|(15[0-9]{9})|(17[0-9]{9})|(18[0-9]{9})|(19[0-9]{9})$/;
            if (phone == null || phone == "") {
                $("#phone_div").addClass("has-error");
                $("#message1").text("手机号不能为空");
                return;
            }
            if (!phone.match(regex)) {
                $("#phone_div").addClass("has-error");
                $("#message1").text("手机号格式错误");
                $('#phone').focus();
                return;
            }

            //验证用户密码
            var name = $("#password").val();
            if (name == null || name == "") {
                $("#password_div").addClass("has-error");
                $("#message2").text("密码不能为空");
                return;
            }

            //提交登录请求
            $.post("${pageContext.request.contextPath}/user/loginByPassword", {
                loginPhone: $("#phone").val(),
                password: $("#password").val()
            }, function (result) {
                if (result == "errorPassword") {
                    $("#password_div").addClass("has-error");
                    $("#message2").text("密码错误");
                    return;
                }

                if (result == "true"){
                    location.replace("/index.jsp");
                }
            });
        });
    });
</script>
</body>

</html>